// pages/index.js
var Api = require("../common/api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    spaceInfo:[],
    spaceNum: 0,

    //当前登录的用户openid
    openid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;

    var openid = wx.getStorageSync('openid');
    _this.setData({
      openid: openid,
    });

    if(openid != null && openid !=""){
      //请求参数封装 
      var data = {
        openid: openid
      }
      var params = JSON.stringify(data)
      Api.getMySpaces(params).then(function (result) {
        _this.setData({
          spaceInfo: result.data.data.spaces,
          spaceNum: result.data.data.spaceNum
        });
      }).catch(function (error) {
        console.log(error)
      })
    }
    
  },

  toCreate:function(){
    if (this.data.openid == null || this.data.openid == ""){
      wx.navigateTo({
        url: "/pages/component/login/login"
      });
    }else{
      wx.navigateTo({
        url: "../component/create/create"
      })
    }
  },


  toArticle:function(){
    if (this.data.openid == null || this.data.openid == "") {
      wx.navigateTo({
        url: "/pages/component/login/login"
      });
    } else {
      wx.navigateTo({
        url: "article/article"
      })
    }
  },
  
})